package com.ruoyi.test.service.impl;

import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.test.dao.PersonDao;
import com.ruoyi.test.domain.Person;
import com.ruoyi.test.service.IPersonServ;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

@Service
@Transactional
public class PersonServImpl implements IPersonServ {
    @Resource
    private PersonDao dao;

    @Override
    public JSONObject login(Person vo) {
        System.out.println(new Throwable().getStackTrace()[0]);
        JSONObject o = new JSONObject();
        o.put("success", false);
        o.put("message", "登录失败！");
        int i = dao.login(vo);
        if (i == 1) {
            o.put("success", true);
            o.put("message", "登录成功！");
            o.put("vo", dao.findByUsername(vo.getUsername()));
        }

        //升级：写入日志。。。。
        return o;
    }

    @Override
    public JSONObject delete(String id) {
        JSONObject o = new JSONObject();
        o.put("success", false);
        o.put("message", "删除操作失败！");
        int i = dao.delete(id);
        if (i > 0) {
            o.put("success", true);
            o.put("message", "删除操作成功！");
        }
        return o;
    }

    @Override
    public Person getOne(String id) {
        System.out.println(new Throwable().getStackTrace()[0]);
        return dao.getOne(id);
    }

    @Override
    public JSONObject findUser(String param) {
        JSONObject o = new JSONObject();
        o.put("success", false);
        o.put("message", "找不到这个人！");
        Person vo = dao.getOne(param);
        if (vo != null) {
            o.put("success", true);
            o.put("message", "找倒起这个人了！");
            o.put("data", vo);
        }
        return o;
    }

    @Override
    public List<Person> listAll() {
        //记录日志
        //TODO
        return dao.listAll();
    }

    @Override
    public JSONObject listAllNg() {
        JSONObject o = new JSONObject();
        o.put("success", false);
        o.put("message", "一个也没有");
        List<Person> all = dao.listAll();
        if (all.size() > 0) {
            o.put("success", true);
            o.put("message", "找倒起个数：" + all.size());
            o.put("data", all);
            //o.put("all", all);
        }
        return o;
    }

    @Override
    public JSONObject update(Person vo) {
        System.out.println(new Throwable().getStackTrace()[0]);
        JSONObject o = new JSONObject();
        o.put("success", false);
        o.put("message", "更新操作失败！");
        int i = dao.update(vo);
        if (i > 0) {
            o.put("success", true);
            o.put("message", "更新操作成功！");
        }
        return o;
    }

    @Override
    public JSONObject add(Person vo) {
        System.out.println(new Throwable().getStackTrace()[0]);
        JSONObject o = new JSONObject();
        o.put("success", false);
        o.put("message", "新增操作失败！");
        int i = dao.add(vo);
        if (i > 0) {
            o.put("success", true);
            o.put("message", "新增操作成功！");
        }
        return o;
    }

    @Override
    public Object newOper() {
        //TODO
        return null;
    }
}
